<template>
  <el-dialog title="新增员工" width="600px" :visible.sync="show" @close="closeEvent">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" value-format="yyyy-MM-dd" placeholder="请输入入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option v-for="item in employeesData.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- 部门选择
              1：展示组织架构
              2：可以选择部门（选择后，关闭组织架构，输入框有选择的组织架构名称）
         -->
        <div v-clickout="outEvent">

          <el-input v-model="form.departmentName" readonly placeholder="请输入部门" @focus="focusEvent" />
          <el-collapse-transition>
            <el-tree v-if="showTree" :data="treeData" :props="{label:'name',children:'children'}" @node-click="nodeClick" />
          </el-collapse-transition>
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" value-format="yyyy-MM-dd" placeholder="请输入转正时间" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button>取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import employeesData from '@/api/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  data() {
    return {
      show: false,
      employeesData,
      treeData: [],
      showTree: false,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		姓名
        mobile: [{ required: true, message: '必填', trigger: 'change' },
          { pattern: /^1[2-9]\d{9}$/, message: '请正确输入手机号', trigger: 'change' }
        ], //	string	非必须		手机号
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		入职时间
        formOfEmployment: [{ required: true, message: '必填', trigger: 'change' }], //	number	非必须		聘用形式
        workNumber: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须		工号
        departmentName: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须		组织名称
      }
    }
  },
  created() {

  },
  methods: {
    // 确定点击
    async submit() {
      await this.$refs.form.validate()
      await sysUserPost(this.form)
      this.show = false
      this.$message.success('新增员工成功')
      this.$emit('getData')
    },
    // 关闭事件
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        timeOfEntry: '', //	string	非必须		入职时间
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        correctionTime: '' //	string	非必须		转正时间
      }
    },
    // 获取组织架构数据
    async  getTreeData() {
      const res = await companyDepartment()
      this.treeData = this.changeData(res.data.depts, '')
      console.log('组织架构数据', res.data.depts)
    },
    // 树形数据转换
    changeData(arr, pid) {
      return arr.filter(item => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    // tree点击
    nodeClick(row) {
      this.form.departmentName = row.name
      this.showTree = false
      console.log(row)
    },
    // 聚焦事件
    focusEvent() {
      if (this.treeData.length === 0) {
        this.getTreeData()
      }
      this.showTree = true
    },
    outEvent() {
      this.showTree = false
    }
  }
}
</script>
<style>
</style>
